﻿#include "consumerdlg.h"
#include "ui_consumerdlg.h"
#include <QDebug>

ConsumerDlg::ConsumerDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ConsumerDlg)
{
    ui->setupUi(this);
    ui->tableWidget->setColumnCount(4);


    ui->tableWidget->horizontalHeader()->setDefaultSectionSize(100);

    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//自适应列宽

    //设置表头内容
    QStringList header;
    header << QString::fromLocal8Bit("操作者") << QString::fromLocal8Bit("用户类型") << QString::fromLocal8Bit("权限等级") << QString::fromLocal8Bit("密码");
    ui->tableWidget->setHorizontalHeaderLabels(header);

    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);  //整行选中的方式
    //    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);   //禁止修改
    //    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);  //设置为可以选中单个

}

ConsumerDlg::~ConsumerDlg()
{
    delete ui;
}

void ConsumerDlg::on_quitBt_clicked()
{
    // 退出
    this->hide();
}

void ConsumerDlg::on_addBt_clicked()
{
    // 添加用户
//    QTableWidgetItem item;
    nCount++;
    ui->tableWidget->setRowCount(nCount);
//    item.setText("231");



    //    ui->tableWidget->setItem(nCount - 1, 0, new QTableWidgetItem(QString::fromLocal8Bit("张三")));
    //    ui->tableWidget->setItem(nCount - 1, 1, new QTableWidgetItem(QString::fromLocal8Bit("管理员")));
    //    ui->tableWidget->setItem(nCount - 1, 2, new QTableWidgetItem("2"));
    //    ui->tableWidget->setItem(nCount - 1, 3, new QTableWidgetItem("1234567"));


//    ui->tableWidget->setItem(nCount - 1, 3, &item);
}

void ConsumerDlg::on_changeBt_clicked()
{
    // 修改用户
}

void ConsumerDlg::on_deleteBt_clicked()
{
    // 删除用户
    ui->tableWidget->clearContents();

    nCount = 0;
    ui->tableWidget->setRowCount(nCount);
    //    QTableWidgetItem * item = ui->tableWidget->currentItem();
    //    if (item == Q_NULLPTR)return;
    //    ui->tableWidget->removeRow(item->row());
    //    int curRow=ui->tableWidget->currentRow();//当前行号
    //    qDebug() << curRow;
    //    for(int i = 0;i < 4;i++){
    //        //        ui->tableWidget->takeItem(curRow,i);
    //        QTableWidgetItem *item = ui->tableWidget->item(curRow,i);

    //        if (item)
    //        {
    //            delete item;
    //        }
    //    }
    //    ui->tableWidget->removeRow(curRow); //删除当前行及其items
    //    nCount--;
}

